/**
 * Created by zqk on 2018/7/5.
 */
var primaryKey;
var templateList;
var bt_list_edit = $('#bt_list_edit').attr('id');
var bt_list_add = $('#bt_list_add').attr('id');
var bt_list_delete = $('#bt_list_delete').attr('id');
$(document).ready(function () {
    var form=new xhForm($('#xh_form'));
    templateList= $('#templateList');
    $('.xh-datagrid-toolbar').parseToolbar();
    //设置禁用
    $('#bt_list_edit').setButtonDisable('eu');
    $('#bt_list_delete').setButtonDisable('eu');
    form.parse();
    form.bindRequired($('#bt_form_add,#bt_form_edit'));//绑定事件
    templateList.treegrid({
        url: moduleUrl+'template/getTemplate',
        method: 'post',
        rownumbers: true,
        pagination: true,
        pageSize: 50,
        pageList: [20,50,100,200],
        idField: 'primaryKey',
        treeField: 'sName',
        singleSelect: true,
        columns:[[
            {field:'ck',checkbox:true},
            {field:'iId',title:'iId',hidden:true},
            {field:'sName',title:'名称',width:100,align:'left',halign:'center'},
            {field:'sContent',title:'内容',width:150,align:'left',halign:'center'},
            {field:'sRemarks',title:'注释',width:200,align:'left',halign:'center'},
            {field:'tCreateDate',title:'创建时间',width:150,align:'center',halign:'center'},
            {field:'tLastModifyDate',title:'最后编辑时间',width:150,align:'center',halign:'center'},
        ]],
        onSelect:function (row) {
            primaryKey = row['primaryKey'];
            templateList.treegrid('expand',primaryKey);
            $('#bt_list_edit').setButtonEnable('eu');//编辑enable
            $('#bt_list_delete').setButtonEnable('eu');//删除enable
        },
        onBeforeSelect:function (row) {
            if(primaryKey==undefined){
                return true;
            }else {
                if(primaryKey==row['primaryKey']){
                    $('#bt_list_edit').setButtonDisable('eu');
                    $('#bt_list_delete').setButtonDisable('eu');
                    templateList.treegrid('unselect',primaryKey);
                    templateList.treegrid('collapse',primaryKey);
                    primaryKey = undefined;
                    return false;
                }
            }
        },
        onBeforeCheck:function (row) {
            if(primaryKey == undefined){
                return true;
            }else {
                if(primaryKey==row['primaryKey']){
                    templateList.treegrid('unselect',primaryKey);
                    templateList.treegrid('uncheckRow',primaryKey);
                    primaryKey = undefined;
                    $('#bt_list_edit').setButtonDisable('eu');
                    $('#bt_list_delete').setButtonDisable('eu');
                    return false;
                }
            }
        },
    });

    $('#bt_list_search').click(function () {
        var sName = $('#sName').getElementValue();//获取input值
        templateList.treegrid('load',{sName:sName});
    });

    $('.toolbar a').click(function () {
        var eleid = $(this).attr('id');
        var obj=templateList.datagrid('getSelections')[0];
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        if(eleid=='bt_list_add'){
            if(obj!=undefined&&obj['type']==2){
                window.top.xhTop.showError('错误','节点下不能创建节点');
            }else if(obj==undefined){
                window.top.xhTop.showModal({
                    title:'新增模板',
                    content: '<iframe src="'+moduleUrl+'template/templateForm"  style="width: 100%;border-style: none;padding: 0px;height: 150px"  scrolling="yes"></iframe>',
                    button:'提交',
                    backClose: false
                });
            }else if(obj!=undefined&&obj['type']==1){
                window.top.xhTop.showModal({
                    title:'新增节点',
                    content: '<iframe src="'+moduleUrl+'template/nodeForm?iTemplateId='+obj['iId']+'"  style="width: 100%;border-style: none;padding: 0px;height: 250px"  scrolling="yes"></iframe>',
                    button:'提交',
                    backClose: false
                });
            }

        } else if(eleid =='bt_list_delete'&&obj!=undefined){
            if(obj['type']==1){
                var text = '确定要删除该模板吗?';
                var url = moduleUrl+'template/delTemplateById';
            }else if(obj['type']==2){
                var text = '确定要删除该节点吗?';
                var url = moduleUrl+'template/delNodeById';
            }
            myWindow.initPopUp({title: '确认删除', text:text},function () {
                $.ajax({
                    url:url,
                    data:{iId:obj['iId']},
                    type:'POST',
                    success:function (data) {
                        window.top.xhTop.showSuccess(
                            data.msg
                        );
                        $('#bt_list_edit').setButtonDisable('eu');
                        $('#bt_list_delete').setButtonDisable('eu');
                        $('#bt_list_add').setButtonEnable('eu');
                        templateList.treegrid('reload');
                    },
                    error:function (data) {
                        window.top.xhTop.showError(
                            data.responseJSON.error,
                            data.responseJSON.msg
                        );
                    }
                });
            })
        } else if (eleid == 'bt_list_edit'&&obj!=undefined){
            if(obj['type']==1){
                window.top.xhTop.showModal({
                    title:'编辑模板',
                    content: '<iframe src="'+moduleUrl+'template/templateForm?iId='+obj['iId']+'"  style="width: 100%;border-style: none;padding: 0px;height: 150px"  scrolling="yes"></iframe>',
                    button:'提交',
                    backClose: false,
                });
            }else if(obj['type']==2){
                window.top.xhTop.showModal({
                    title:'编辑节点',
                    content: '<iframe src="'+moduleUrl+'template/nodeForm?iId='+obj['iId']+'"  style="width: 100%;border-style: none;padding: 0px;height: 350px"  scrolling="yes"></iframe>',
                    button:'提交',
                    backClose: false,
                });
            }

        }
    });


});

